//
//  GGTBusinessViewController.m
//  GGT
//
//  Created by 空空空 on 14/11/4.
//  Copyright (c) 2014年 GGT. All rights reserved.
//

#import "GGTBusinessViewController.h"
#import "GGTAppDelegate.h"
#import "DPRequest.h"
#import "GGTBusinessDetailModel.h"
#import "GGTSingleBusinessTableViewCell.h"
#import "GGTTeamBuyDetailModel.h"
#import "GGTGetBusinessTableViewCell.h"
#import "GGTTeamBuyReviewModel.h"
#import "GGTTeamBuyDetailTableViewCell.h"
#import "GGTWriteReviewTableViewCell.h"
#import "GGTTeamBuyDetailViewController.h"
@interface GGTBusinessViewController ()<UITableViewDataSource, UITableViewDelegate, DPRequestDelegate, UIActionSheetDelegate>
@property (nonatomic, retain)UITableView *businessTabView;
@property (nonatomic, retain)GGTBusinessDetailModel *business;
@property (nonatomic, retain)GGTSingleBusinessModel *busies;
@property (nonatomic, retain)NSString *address;
@property (nonatomic, retain)NSNumber *phone;
@property (nonatomic, retain)NSMutableArray *otherTeamBuy;
@property (nonatomic, assign)int flag;
@property (nonatomic, retain)GGTTeamBuyReviewModel *teamBuyReview;
@end

@implementation GGTBusinessViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.otherTeamBuy = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    self.flag = 0;
    self.businessTabView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.businessTabView.dataSource = self;
    self.businessTabView.delegate = self;
    [self.view addSubview:self.businessTabView];
    NSString *url = @"v1/business/get_single_business";
    NSString *params = [NSString stringWithFormat:@"business_id=%@",self.businessDetail];
    [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
    
}

- (void)request:(DPRequest *)request didFailWithError:(NSError *)error
{
    //self.resultTextView.contentOffset = CGPointZero;
    NSLog(@"注意参数或地址填写错误:%@",[error description]);
    
}

- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result
{
    //self.resultTextView.contentOffset = CGPointZero;
    if (self.flag % 3 == 0) {
        NSDictionary *dic = [[result objectForKey:@"businesses"]lastObject];
        GGTSingleBusinessModel *business = [[GGTSingleBusinessModel alloc]initWithDictionary:dic];
        self.busies = business;
        self.phone = business.telephone;
        self.address = business.address;
        NSString *url = @"v1/deal/get_deals_by_business_id";
        NSString *params = [NSString stringWithFormat:@"business_id=%@&city=%@",self.businessDetail, [[NSUserDefaults standardUserDefaults]objectForKey:@"cityName"]];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
        
    } else if (self.flag % 3 == 1) {
        NSMutableArray *arr = [result objectForKey:@"deals"];
        for (NSDictionary *dic in arr) {
            GGTTeamBuyDetailModel *detail = [[GGTTeamBuyDetailModel alloc]initWithDictionary:dic];
            [self.otherTeamBuy addObject:detail];
        }
        NSString *url = @"v1/review/get_recent_reviews";
        NSString *params = [NSString stringWithFormat:@"business_id=%@&limit=1",self.businessDetail];
        [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
    } else if (self.flag % 3 == 2) {
        NSMutableArray *arr1 = [result objectForKey:@"reviews"];
        NSDictionary *dic = [arr1 lastObject];
        GGTTeamBuyReviewModel *review1 = [[GGTTeamBuyReviewModel alloc]initWithDictionary:dic];
        self.teamBuyReview = review1;
    }
    self.flag++;
    [self.businessTabView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    } else if (section == 1) {
        return [self.otherTeamBuy count];
    } else if (section == 2) {
        return 2;
    } else if (section == 3) {
        return 1;
    } else {
        return 3;
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            GGTSingleBusinessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"aa"];
            if (cell == nil) {
                cell = [[[GGTSingleBusinessTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"aa"]autorelease];
            }
            cell.singleBusiness = self.busies;
    
            return cell;
        } else if(indexPath.row == 1) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"11"];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"11"]autorelease];
            }
            
            cell.textLabel.text = self.address;
            return cell;
        } else {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"22"];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"22"]autorelease];
            }
            
            
            cell.textLabel.text = [NSString stringWithFormat:@"%@", self.phone];
            return cell;
        }
        
    } else if (indexPath.section == 1) {
        GGTGetBusinessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bb"];
        if (cell == nil) {
            cell = [[[GGTGetBusinessTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"bb"]autorelease];
        }
        GGTTeamBuyDetailModel *detail = [self.otherTeamBuy objectAtIndex:indexPath.row];
        cell.getBusiness = detail;
        return  cell;
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            GGTTeamBuyDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cc"];
            if (cell == nil) {
                cell = [[[GGTTeamBuyDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cc"]autorelease];
            }
            cell.teamBuyReview = self.teamBuyReview;
            return cell;
        } else {
            GGTWriteReviewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"33"];
            if (cell == nil) {
                cell = [[[GGTWriteReviewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"33"]autorelease];
            }
            
            return cell;
        }
        
        
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dd"];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"dd"]autorelease];
        }
        return cell;
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    } else {
        return 30;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 120;
        } else {
            return 60;
        }
    } else if (indexPath.section == 1) {
        return 80;
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            return 80;
        } else {
            return 140;
        }
    } else {
        return 60;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        GGTTeamBuyDetailViewController *teamBuy = [[GGTTeamBuyDetailViewController alloc]init];
        teamBuy.GGTTeamBuyDetail = [self.otherTeamBuy objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:teamBuy animated:YES];
        [teamBuy release];
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 2) {
            NSString *str = [NSString stringWithFormat:@"%@", self.phone];
            UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:str otherButtonTitles:nil, nil];
            [actionSheet showInView:self.view];

        }
    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSString *str = [NSString stringWithFormat:@"tel://%@", self.phone];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    } else {
        
    }
}
- (void)dealloc
{
    [_businessTabView release];
    [_business release];
    [_businessDetail release];
    [_address release];
    [_phone release];
    [_otherTeamBuy release];
    [super dealloc];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
